package com.example.myapplication.ui.news.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.myapplication.R;
import com.example.myapplication.app.AppConstant;
import com.example.myapplication.base.BaseActivity;
import com.example.myapplication.ui.wight.PullBackLayout;
import com.example.myapplication.utils.MyUtils;
import com.example.myapplication.utils.SystemUiVisibilityUtil;
import com.example.myapplication.utils.statarBar.StatusBarCompat;

import butterknife.BindView;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class GrilActivity extends BaseActivity implements PullBackLayout.Callback {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.photo_touch_iv)
    PhotoView photoTouchIv;
    @BindView(R.id.pull_back_layout)
    PullBackLayout pullBackLayout;

    @BindView(R.id.background)
    RelativeLayout background;

    private ColorDrawable mBackground;

    private boolean mIsToolBarHidden;
    private boolean mIsStatusBarHidden;

    public static void startAction(Context context, String url){
        Intent intent = new Intent(context, GrilActivity.class);
        intent.putExtra(AppConstant.PHOTO_DETAIL,url);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        setSupportActionBar(toolbar);
        return R.layout.activity_gril;
    }

    @Override
    protected void initView() {
        StatusBarCompat.translucentStatusBar(this);
        pullBackLayout.setCallback(this);
        toolBarFadeIn();
        initToolbar();
        initBackground();
        loadPhotoIv();
        loadPhotoIv();
        setPhotoViewClickEvent();
    }


    private void initToolbar() {
        toolbar.setTitle(getString(R.string.girl_title));
        toolbar.setNavigationOnClickListener(view -> finish());
    }
    private void setPhotoViewClickEvent() {
        photoTouchIv.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float v, float v1) {
                hideOrShowToolbar();
                hideOrShowStatusBar();
            }
        });
    }

    private void loadPhotoIv() {
        String url = getIntent().getStringExtra(AppConstant.PHOTO_DETAIL);
        Glide.with(this).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ic_empty_picture)
                .crossFade().into(photoTouchIv);
    }
    private void initBackground() {
        mBackground = new ColorDrawable(Color.BLACK);
        MyUtils.getRootView(this).setBackgroundDrawable(mBackground);
    }

    private void toolBarFadeIn() {
        mIsToolBarHidden = true;
        hideOrShowToolbar();
    }
    protected void hideOrShowToolbar() {
        toolbar.animate()
                .alpha(mIsToolBarHidden ? 1.0f : 0.0f)
                .setInterpolator(new DecelerateInterpolator(2))
                .start();
        mIsToolBarHidden = !mIsToolBarHidden;
    }
    private void toolBarFadeOut() {
        mIsToolBarHidden = false;
        hideOrShowToolbar();
    }

    private void hideOrShowStatusBar() {
        if (mIsStatusBarHidden) {
            SystemUiVisibilityUtil.enter(GrilActivity.this);
        } else {
            SystemUiVisibilityUtil.exit(GrilActivity.this);
        }
        mIsStatusBarHidden = !mIsStatusBarHidden;
    }

    @Override
    public void onPullStart() {
        toolBarFadeOut();

        mIsStatusBarHidden = true;
        hideOrShowStatusBar();
    }

    @Override
    public void onPull(float progress) {
        progress = Math.min(1f, progress * 3f);
        mBackground.setAlpha((int) (0xff/*255*/ * (1f - progress)));
    }

    @Override
    public void onPullCancel() {
        toolBarFadeIn();
    }

    @Override
    public void onPullComplete() {
        supportFinishAfterTransition();
    }

    @Override
    public void supportFinishAfterTransition() {
        super.supportFinishAfterTransition();
    }
}
